package com.eDriveV2;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapViewer extends MapActivity {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private List<Overlay> mapOverlays;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mapslayout);
		
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		String fileName = getIntent().getExtras().getString("fileName");

		XMLReader reader = new XMLReader(fileName);
		ArrayList[] coordinates = reader.readXML();

		
		
		mapOverlays = mapView.getOverlays();
		Drawable drawableGreen = this.getResources().getDrawable(R.drawable.bulletpoint);
		Drawable drawableRed = this.getResources().getDrawable(R.drawable.bulletpoint_red);
		RouteOverlay itemizedoverlay = new RouteOverlay(drawableGreen);
		RouteOverlayRed itemizedoverlayRed = new RouteOverlayRed(drawableRed);
		
		ArrayList<String[]> coord = new ArrayList<String[]>();
		
		
		
		for (int i = 0; i < coordinates[1].size(); i++) {
			String latAndLong = (String) coordinates[1].get(i);
			coord.add(latAndLong.split(","));
		}
		
		int[] coor = getCoordinates(Double.parseDouble(coord.get(0)[0]), Double.parseDouble(coord.get(0)[1]));
		
		MapController mc = mapView.getController();
		
		boolean firstRun = true;
		
		for(int i = 0; i < coord.size(); i++){
			int[] coordo = getCoordinates(Double.parseDouble(coord.get(i)[0]), Double.parseDouble(coord.get(i)[1]));
			GeoPoint points = new GeoPoint(coordo[0],coordo[1]);
			OverlayItem oi = new OverlayItem(points, "", "");
			if(coordinates[4].get(i).equals("A") || coordinates[4].get(i).equals("B") || coordinates[4].get(i).equals("C")){
				itemizedoverlay.addOverlay(oi);
				mapOverlays.add(itemizedoverlay);
			}
			else {
				itemizedoverlayRed.addOverlay(oi);
				mapOverlays.add(itemizedoverlayRed);
			}
			if(firstRun){
				mc.animateTo(points);
				mc.setZoom(18);
				firstRun = false;
			}
		}
		coordinates[1].clear();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private int[] getCoordinates(double lat, double longt) {
		int[] coor = new int[2];
		coor[0] = (int) (lat * Math.pow(10, 6));
		coor[1] = (int) (longt * Math.pow(10, 6));
		return coor;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mapsmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.homeItem:
			showHome();
			return true;
		case R.id.settingsItem:
			showSettings();
			return true;
		case R.id.statViewItem:
			showFileOverview();
			return true;
		case R.id.helpItem:
			showInfoScreen();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void showHome() {
		Intent processorIntent = new Intent(this, Processor.class);

		startActivity(processorIntent);
	}

	private void showFileOverview() {
		Intent fileOverviewIntent = new Intent(this, FileOverview.class);

		startActivity(fileOverviewIntent);
	}

	/*
	 * Method called by pressing 'Settings' in the options menu. Opens the
	 * settings window.
	 */
	private void showSettings() {
		Toast.makeText(getApplicationContext(),
				"Currently no settings available", 15).show();
	}

	/*
	 * Show the infoscreen in an PopupWindow.
	 */
	private void showInfoScreen() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.infoText)
				.setCancelable(false)
				.setNegativeButton("Close",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK))
	    {
	       mapOverlays.clear();
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
